Adding and removing columns from a data frame
Problem
You want to add or remove columns from a data frame.
Solution
There are many different ways of adding and removing columns from a data frame.
data <- data.frame (id=1:3, weight=c(20,27,24)) # id weight # 1 20 # 2 27 # 3 24 # Ways to add a column data$size <- c("small", "large", "medium") data[["size"]] <- c("small", "large", "medium") data[,"size"] <- c("small", "large", "medium") data$size <- 0 # Ways to remove the column data$size <- NULL data[["size"]] <- NULL data[,"size"] <- NULL data[,3] <- NULL data <- subset(data, select=-size)